﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;
using TarreOA.TarreValidationCode;

public partial class admin_TalkHouseSetting_Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.DataList_DataTable();
        }
    }

    private void DataList_DataTable()
    {
        TalkHouseListService thlService = new TalkHouseListService();
        DataTable dt = new DataTable();
        dt.Columns.Add("TalkHouseId", typeof(string));
        dt.Columns.Add("TalkHouseName", typeof(string));
        dt.Columns.Add("TalkHouseLastUpTime", typeof(string));
        dt.Columns.Add("TalkHouseOpenState", typeof(string));
        List<TalkHouseList> list = thlService.findALL();
        if (list != null && list.Count > 0)
        {
            foreach (TalkHouseList thl in list)
            {
                DataRow dr = dt.NewRow();
                dr["TalkHouseId"] = thl.TalkHouseId.ToString();
                dr["TalkHouseName"] = thl.TalkHouseName;
                dr["TalkHouseLastUpTime"] = thl.TalkHouseLastUpTime.ToString();
                dr["TalkHouseOpenState"] = thl.TalkHouseOpenState == 0 ? "<font color='green'>开放</font>" : "<font color='red'>关闭</font>";
                dt.Rows.Add(dr);
            }
        }
        if (dt.Rows.Count > 0)
        {
            this.DataList1.DataSource = dt.DefaultView;
        }
        else
        {
            this.DataList1.DataSource = null;
        }
        this.DataList1.DataBind();
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ((LinkButton)e.Item.FindControl("LbtDel")).Attributes.Add("onclick", "return confirm('您确认删除此聊天室吗？')");
            ((LinkButton)e.Item.FindControl("LbtDelTalk")).Attributes.Add("onclick", "return confirm('您确认删除此聊天室的所有聊天记录吗？')");
            string state = ((Label)e.Item.FindControl("LblState")).Text;
            if ("<font color='green'>开放</font>".Equals(state))
            {
                ((LinkButton)e.Item.FindControl("LbtOpen")).Visible = false;
            }
            else
            {
                ((LinkButton)e.Item.FindControl("LbtClose")).Visible = false;
            }
        }
    }
    protected void IbtCreate_Click(object sender, EventArgs e)
    {
        if (Request.Form.Get(this.TxtValidatorCode.ClientID).Trim().Equals(Code.Codes))
        {
            if (!"".Equals(Request.Form.Get(this.TxtAreaName.ClientID)))
            {
                string thlName = Request.Form.Get(this.TxtAreaName.ClientID);
                string thlWelcome = Request.Form.Get(this.TxtWelcome.ClientID);
                string thlTheme = Request.Form.Get(this.TxtTalkTheme.ClientID);
                TalkHouseListService thilService = new TalkHouseListService();
                if (thilService.SaveTalkHouseInfo(thlName, thlWelcome, thlTheme, DateTime.Now) == 1)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('聊天室创建成功！');", true);
                    this.TxtAreaName.Text = "";
                    this.TxtValidatorCode.Text = "";
                    this.DataList_DataTable();
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('创建出现错误，请重试！');", true);
                }
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('验证码输入错误！');", true);
        }
    }
    protected void LbtOpen_Click(object sender, EventArgs e)
    {
        int thl_id = int.Parse(((LinkButton)sender).Attributes["thl_id"]);
        TalkHouseListService thlService = new TalkHouseListService();
        thlService.UpdateTalkHouseOpenState(0, thl_id);
        this.DataList_DataTable();
    }
    protected void LbtClose_Click(object sender, EventArgs e)
    {
        int thl_id = int.Parse(((LinkButton)sender).Attributes["thl_id"]);
        TalkHouseListService thlService = new TalkHouseListService();
        thlService.UpdateTalkHouseOpenState(1, thl_id);
        this.DataList_DataTable();
    }
    protected void LbtDelTalk_Click(object sender, EventArgs e)
    {
        string thl_id = ((LinkButton)sender).Attributes["thl_id"];
        UserTalkContent utc = UserTalkContent.instance();
        if (utc.TalkContents["Talk" + thl_id] != null)
        {
            List<string> contentList = (List<string>)utc.TalkContents["Talk" + thl_id];
            contentList.Clear();
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('聊天记录删除成功！');", true);
        }
    }
    protected void LbtEdit_Click(object sender, EventArgs e)
    {
        string thl_Id = ((LinkButton)sender).Attributes["thl_Id"];
        Response.Redirect("TalkHouseEdit.aspx?thl_Id=" + thl_Id);
    }
    protected void LbtDel_Click(object sender, EventArgs e)
    {
        int thl_id = int.Parse(((LinkButton)sender).Attributes["thl_id"]);
        TalkHouseListService thlService = new TalkHouseListService();
        thlService.DelTalkHouseInfoById(thl_id);
        this.DataList_DataTable();
    }
}
